package com.asiainfo.work.utils;

import java.io.Serializable;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

/**
 * 返回基类
 * @author: bf
 * @date: 2021/1/5-10:20
 */
@Data
@AllArgsConstructor
@Builder
public class BaseResponse<T> implements Serializable {

    private static final long serialVersionUID = 2514076590560325794L;

    private String code;

    private String message;

    private LocalDateTime timestamp = LocalDateTime.now();

    private T data;

    @Deprecated
    public BaseResponse() {
        this.message = BaseResultCode.SUCCESS.getMsg();
        this.code = BaseResultCode.SUCCESS.getCode();
    }

    @Deprecated
    public BaseResponse(T data) {
        this.message = BaseResultCode.SUCCESS.getMsg();
        this.code = BaseResultCode.SUCCESS.getCode();
        this.data = data;
    }

    public static <T> BaseResponse<T> success() {
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(BaseResultCode.SUCCESS.getCode());
        response.setMessage(BaseResultCode.SUCCESS.getMsg());
        return response;
    }

    public static <T> BaseResponse<T> success(T data) {
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(BaseResultCode.SUCCESS.getCode());
        response.setMessage(BaseResultCode.SUCCESS.getMsg());
        response.setData(data);
        return response;
    }

    public static <T> BaseResponse<T> failure(ErrorResultCode errorResultCode) {
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(errorResultCode.getCode());
        response.setMessage(errorResultCode.getMsg());
        return response;
    }

    @Deprecated
    public static <T> BaseResponse<T> failure(String code, String message) {
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(code);
        response.setMessage(message);
        return response;
    }

    @Deprecated
    public static <T> BaseResponse<T> failure(String message) {
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(BaseResultCode.FAILURE.getCode());
        response.setMessage(message);
        return response;
    }

    @Deprecated
    public static <T> BaseResponse<T> failure(BaseResultCode resultCode) {
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(resultCode.getCode());
        response.setMessage(resultCode.getMsg());
        return response;
    }

    @Deprecated
    public static <T> BaseResponse<T> failure(BaseResultCode resultCode, T data) {
        BaseResponse<T> response = BaseResponse.failure(resultCode);
        response.setData(data);
        return response;
    }

    public boolean isSuccess() {
        return BaseResultCode.SUCCESS.getCode().equals(code);
    }
}
